package com.city.parking.util;

import android.app.Activity;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;

import com.city.parking.R;

public class KeyboardUtil {
	private EditText ed;
	public boolean isletter = false;
	private Keyboard region_kv;
	private Keyboard letter_kv;
	private KeyboardView keyboardView;
	private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
		public void onKey(int primaryCode, int[] keyCodes) {
			Editable editable = ed.getText();
			int i = ed.getSelectionStart();
			if (!isletter) {
                keyboardView.setKeyboard(letter_kv);
                isletter = true;
			}

			if (primaryCode == Keyboard.KEYCODE_CANCEL || primaryCode == Keyboard.KEYCODE_DONE)
				hideKeyboard();
			else if (primaryCode == Keyboard.KEYCODE_DELETE) {
                if ((editable != null) && (editable.length() > 0) && (i > 0))
                    editable.delete(i - 1, i);
                if (editable.length() == 0 && isletter) {
                    keyboardView.setKeyboard(region_kv);
                    isletter = false;
                }
			} else {
				switch (primaryCode) {
                default:
                    editable.insert(i, Character.toString((char) primaryCode));
                    break;
                case 123:
                    editable.insert(i, "琼");
                    break;
                case 101:
                    editable.insert(i, "皖");
                    break;
                case 111:
                    editable.insert(i, "鄂");
                    break;
                case 109:
                    editable.insert(i, "豫");
                    break;
                case 117:
                    editable.insert(i, "陕");
                    break;
                case 100:
                    editable.insert(i, "渝");
                    break;
                case 121:
                    editable.insert(i, "粤");
                    break;
                case 113:
                    editable.insert(i, "桂");
                    break;
                case 118:
                    editable.insert(i, "吉");
                    break;
                case 105:
                    editable.insert(i, "黑");
                    break;
                case 108:
                    editable.insert(i, "鲁");
                    break;
                case 102:
                    editable.insert(i, "苏");
                    break;
                case 107:
                    editable.insert(i, "冀");
                    break;
                case 115:
                    editable.insert(i, "晋");
                    break;
                case 110:
                    editable.insert(i, "赣");
                    break;
                case 99:
                    editable.insert(i, "沪");
                    break;
                case 97:
                    editable.insert(i, "京");
                    break;
                case 98:
                    editable.insert(i, "津");
                    break;
                case 104:
                    editable.insert(i, "辽");
                    break;
                case 103:
                    editable.insert(i, "浙");
                    break;
                case 106:
                    editable.insert(i, "湘");
                    break;
                case 125:
                    editable.insert(i, "川");
                    break;
                case 120:
                    editable.insert(i, "云");
                    break;
                case 119:
                    editable.insert(i, "贵");
                    break;
                case 116:
                    editable.insert(i, "蒙");
                    break;
                case 127:
                    editable.insert(i, "闽");
                    break;
                case 114:
                    editable.insert(i, "甘");
                    break;
                case 122:
                    editable.insert(i, "青");
                    break;
                case 126:
                    editable.insert(i, "宁");
                    break;
                case 112:
                    editable.insert(i, "新");
                    break;
                case 124:
                    editable.insert(i, "藏");
				}
			}
		}

		public void onPress(int primaryCode) {
		}

		public void onRelease(int primaryCode) {
		}

		public void onText(CharSequence text) {
		}

		public void swipeDown() {
		}

		public void swipeLeft() {
		}

		public void swipeRight() {
		}

		public void swipeUp() {
		}
	};

	public KeyboardUtil(Activity act, EditText edit) {
		this.ed = edit;
		this.region_kv = new Keyboard(act, R.xml.region);
		this.letter_kv = new Keyboard(act, R.xml.letter);
		this.keyboardView = ((KeyboardView) act.findViewById(R.id.keyboard_view));
		this.keyboardView.setKeyboard(this.region_kv);
		this.keyboardView.setEnabled(true);
		this.keyboardView.setPreviewEnabled(true);
		this.keyboardView.setOnKeyboardActionListener(this.listener);
	}

	public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();  
        if (visibility == View.VISIBLE) {  
            keyboardView.setVisibility(View.INVISIBLE);  
        }
	}

	public void showKeyboard() {
        int visibility = keyboardView.getVisibility();  
        if (visibility == View.GONE || visibility == View.INVISIBLE) {  
            keyboardView.setVisibility(View.VISIBLE);  
        }
	}

	public int stateKeyboard() {
		return keyboardView.getVisibility();
	}
}